package cn.springcloud.alibaba.security.handler;

import cn.springcloud.alibaba.core.util.ResponseUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {

	@Override
	public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
		log.error(e.getMessage(), e);

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("code", 403);
		resultMap.put("message", "权限不足，请联系管理员!");
		ResponseUtil.outputJsonResponse(response, resultMap);
	}
}
